/*
 * SerialReader.h
 *
 *
 *  Created on: Oct 8, 2011
 *  Author: Mario H. Voorsluys
 *	email: forcaeluz@gmail.com
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, visit:
 * http://www.gnu.org/copyleft/gpl.html
 */

#ifndef SERIALREADER_H_
#define SERIALREADER_H_

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>

//using namespace std;
/**
 * Communication between the computer and the CAO board, throught a serial port.
 */
class SerialReader {
public:
	/**
	 * Node Constructor
	 * A port name can be given with it. Otherwise, ttyUSB0 will be used.
	 */
	SerialReader();
	/**
	 * Open the connection with the device.
	 * @param device The file location from the port. Default is /dev/ttyUSB0
	 */
	bool openConnection(char* device = "/dev/ttyUSB0");
	/**
	 *
	 */
	void closeConnection();
	/**
	 *
	 */
	bool writeTWIData(unsigned char id, unsigned char reg, unsigned char data);
	/**
	 *
	 */
	int readTWIData(unsigned char id, unsigned char reg);
	/**
	 *
	 */
	int deviceId();
	/**
	 *
	 */
	virtual ~SerialReader();

protected:
	/**
	 *
	 */
	int sendCommand(unsigned char command, int addr, unsigned char opt1=0x00, unsigned char opt2=0x00);
	/**
	 *
	 */
	int checkMessage();
	int device_id;
	struct termios config, old_config;
	int fd;
	char* port_name;
	unsigned char input_buffer[10];

};

#endif /* SERIALREADER_H_ */
